package cn.edu.sptc.sms_lichaoping.controller;

import cn.edu.sptc.sms_lichaoping.entity.Student;
import cn.edu.sptc.sms_lichaoping.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
@Tag(name = "学生管理", description = "学生的CRUD（增删查改）操作")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/query/{id}")
    @Operation(summary = "查询", description = "根据学生ID查询单个学生信息{路径参数形式}")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "id有误"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Map<String, Object> queryStudentByID(@PathVariable @Parameter(
            description = "学生ID", required = true, example = "1", in = ParameterIn.PATH) String id) {
        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("mag", "成功");
        map.put("data", studentService.queryByID(Long.parseLong(id)));
        return map;
    }

    @GetMapping("/query")
    @Parameters(
            @Parameter(description = "学生ID", required = true, example = "1", in = ParameterIn.QUERY)
    )
    @Operation(summary = "查询", description = "根据学生ID查询单个学生信息{请求参数形式}")
    public Map<String, Object> queryStudentByIDParam(@RequestParam String id) {
        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("mag", "成功");
        map.put("data", studentService.queryByID(Long.parseLong(id)));
        return map;
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询", description = "查询所有学生信息")
    public Map<String, Object> queryStudentByAll() {
        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("mag", "成功");
        map.put("data", studentService.queryByAll());
        return map;
    }

    @PostMapping(value = "/save1", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "插入", description = "通过JSON形式插入学生信息")
    public Map<String, Object> insertJSON(
            @RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "传入学生信息，学生ID，创建和更新时间不需要", required = true) Student student
    ) {
        Map map = new HashMap();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if (studentService.insertStudent(student)) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping(value = "/save2", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "插入", description = "通过表单形式插入学生信息")
    public Map<String, Object> insertByForm(Student student) {
        Map map = new HashMap();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if (studentService.insertStudent(student)) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> updateByJSON(@RequestBody Student student) {
        Map map = new HashMap();
        student.setUpdateTime(new Date());
        if (studentService.updateStudent(student)) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String, Object> updateByForm(Student student) {
        Map map = new HashMap();
        student.setUpdateTime(new Date());
        if (studentService.updateStudent(student)) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }

    @GetMapping("/delete/{id}")
    @Parameters(
            @Parameter(description = "学生ID", required = true, example = "1", in = ParameterIn.PATH)
    )
    public Map<String, Object> deleteByID(@PathVariable String id) {
        Map map = new HashMap();
        if (studentService.deleteStudent(Long.parseLong(id))) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }

    @GetMapping("/delete")
    @Parameters(
            @Parameter(description = "学生ID", required = true, example = "1", in = ParameterIn.QUERY)
    )
    public Map<String, Object> deleteByIDParam(@RequestParam String id) {
        Map map = new HashMap();
        if (studentService.deleteStudent(Long.parseLong(id))) {
            map.put("code", "200");
            map.put("msg", "成功");
        } else {
            map.put("code", "201");
            map.put("msg", "失败");
        }
        return map;
    }
}